﻿using System;
using System.Collections;
using System.Collections.Generic;
using CXedu.Model;
using Ajeelee.Core.Database;
using Ajeelee.Core.Utils;

namespace CXedu.Business
{
    public interface IArticleBusinessFactory
    {
        IArticleBusiness CreateArticleBusiness();
    }
    public class ArticleBusinessFactory: BusinessFactory, IArticleBusinessFactory
    {
        public IArticleBusiness CreateArticleBusiness()
        {
            return new Default.ArticleBusiness();
        }    
    }
    ///<summary>
    /// interface
    ///</summary>
    public interface IArticleBusiness : IBusinessBase<Article, int>
    {
        ///<summary>
        ///根据ID值读取Article实体
        ///</summary>
        Article GetById(int id);
        ///<summary>
        ///根据Article实体插入数据库
        ///</summary>
        object Insert(Article entity);
        ///<summary>
        ///根据Article实体更新数据库
        ///</summary>
        int Update(Article entity);
        ///<summary>
		///<summary>
        ///根据HashTable更新数据库(只更新指定字段)
        ///</summary>
        int UpdateSomeVal(Hashtable table);        
        ///<summary>
        ///根据Article实体删除数据库
        ///</summary>
        int Delete(Article entity);
        ///<summary>
        ///根据Article实体ID删除数据库
        ///</summary>
        int Delete(int id);
        /// <summary>
        /// 读取所有记录
        /// </summary>
        IList<Article> GetAllArticle();
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        IList<Article> GetAllArticle(object objQuery);
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录(先查所有再用List手动处理分页)
        /// </summary>
        IList<Article> GetAllArticle(object objQuery, PageStructure page);
		/// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
		IList<Article> GetAllArticleByPage(Hashtable table, PageStructure page);
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        int GetAllCount(Hashtable table);
		
    }
}
namespace CXedu.Business.Default
{
    ///<summary>
    ///
    ///</summary>
    public class ArticleBusiness : BusinessBase<Article, int>, IArticleBusiness
    {
	
        ///<summary>
        ///根据ID值读取Article实体
        ///</summary>
        public Article GetById(int id)
        {
            if(id == default(Int32)) 
                throw new BusinessException("Invalid_Id_Value");
            Article entity = GetById("Article.Article_GetById", id);
            return entity;
        }
        ///<summary>
        ///根据Article实体插入数据库
        ///</summary>
        public object Insert(Article entity)
        {
            if (entity.Cateid == default(Int32))
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Insert("Article.Article_Insert", entity);
        }
        ///<summary>
        ///根据Article实体更新数据库
        ///</summary>
        public int Update(Article entity)
        {
            if (entity.Cateid == default(Int32))
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Update("Article.Article_Update", entity);
        }
        ///<summary>
        ///根据Hashtable实体更新数据库(只更新指定字段)
        ///</summary>
        public int UpdateSomeVal(Hashtable entity)
        {
            return Update("Article.Article_UpdateSomeVal", entity);
        }
        ///<summary>
        ///根据Article实体删除数据库
        ///</summary>
        public int Delete(Article entity)
        {
            if (entity.Cateid == default(Int32))
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Delete("Article.Article_Delete", entity);
        }
        ///<summary>
        ///根据Article实体ID删除数据库
        ///</summary>
        public int Delete(int id)
        {
            if (id == default(Int32))
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            Article entity = new Article { Cateid = id };
            entity.Cateid = id;
            return Delete("Article.Article_Delete", entity);
        }
        /// <summary>
        /// 读取所有记录
        /// </summary>
        public IList<Article> GetAllArticle()
        {
            return GetAllArticle(null);
        }
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public IList<Article> GetAllArticle(object objQuery)
        {
            return GetAllArticle(objQuery, null);
        }
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录
        /// </summary>
        public IList<Article> GetAllArticle(object objQuery, PageStructure page)
        {
            return QueryForList("Article.Article_GetAll", objQuery, page);
        }
		/// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
		public IList<Article> GetAllArticleByPage(Hashtable table, PageStructure page)
        {
			if (page.CurrPage == 0 || page.PageSize == 0)
			{
				return new List<Article>();
			}
			else if (page.CurrPage == 1)
			{
            	table.Add("TopNum", page.PageSize);
				return GetAllArticle(table);
			}
			
			page.Counts = GetAllCount(table);
            page.PageCount = (page.Counts % page.PageSize == 0) ? page.Counts / page.PageSize : page.Counts / page.PageSize + 1;
			
			int max = (page.CurrPage - 1) * page.PageSize;
			if (max < 500)
            {
            	table.Add("maxsize", max);
				table.Add("pagesize", page.PageSize);
				return QueryForList("Article.Article_GetByPageNotIn", table, null);
            }
			else
			{
				table.Add("pagesize", page.Counts - max > page.PageSize ? page.PageSize : page.Counts - max);
				table.Add("currcount", page.CurrPage * page.PageSize);
				if (table.ContainsKey("OrderDirection"))
                {
                    table["OrderDirection"] = table["OrderDirection"].ToString().ToUpper();
                }
				return QueryForList("Article.Article_GetByPage3Top", table, null);
			}
        }
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public int GetAllCount(Hashtable table)
        {
            return Convert.ToInt32(QueryForScalar("Article.Article_GetAllCount", table));
        }
    }
}
